import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const types= {
  SET_IS_AUTNENTIATED: 'SET_IS_AUTNENTIATED', // 是否认证通过
  SET_USER: 'SET_USER' // 用户信息
}

const state ={//需要维护的状态
  isAutnenticated: false, //是否认证
  user:{}, //存储用户信息
}

const getters ={
  //获取信息
  isAutnenticated: state => state.isAutnenticated,
  user: state => state.user

}

const mutations ={
 //修改信息
 [types.SET_IS_AUTNENTIATED](state, isAutnenticated) {
  if (isAutnenticated)
    state.isAutnenticated = isAutnenticated
  else
    state.isAutnenticated = false
 },
 [types.SET_USER](state, user) {
  if(user)
  state.user = user //用户存在赋值 user
  else
  state.user = {}//用户不存在给个空对象
 }

}
//异步操作
const actions ={
  setIsAutnenticated: ({ commit }, isAutnenticated) => {
    commit(types.SET_IS_AUTNENTIATED, isAutnenticated)
  },
  //用户信息修改
  setUser: ({ commit }, user) => {
    commit(types.SET_USER, user)
  },
  //个人信息   退出
  clearCurrentState: ({ commit }) => {
    commit(types.SET_IS_AUTNENTIATED, false)
    commit(types.SET_USER,null)//用户信息
  }
}
export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions,

})
